/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.action;

import cn.tom.kit.StringUtil;
import com.cunyu.bean.Form;
import com.cunyu.service.AppService;
import com.cunyu.service.BannerService;
import com.cunyu.service.CityService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * banner 管理
 * @author win
 * 2019/04/25-17:18
 */
@Controller
public class SystemController extends BaseController{

    @Resource
    CityService cityService ;

    @Resource
    BannerService bannerService ;

    @Resource
    AppService appService ;

    /**
     * banner 列表
     * @return
     */
    @RequestMapping("/system/banners")
    public String banners(Model model){
        List<Map<String, Object>> banners = bannerService.banners();
        model.addAttribute("respData",banners) ;
        return "system/banners" ;
    }

    /**
     * banner 列表
     * @return
     */
    @RequestMapping("/system/config")
    public String configs(Model model){
        List<Map<String, Object>> configs = appService.configs() ;
        model.addAttribute("respData",configs) ;
        return "system/app" ;
    }

    /**
     * 获取对应banner 数据
     */
    @RequestMapping("/system/banner")
    public void banner(HttpServletResponse res, String id){
        int i = Integer.valueOf(id);
        Map<String, Object> banner = bannerService.getBanner(i);
        printOK(res,"success",banner);
        // ajax json 数据
    }

    /**
     * 获取对应banner 数据
     */
    @RequestMapping("/system/configItem")
    public void config(HttpServletResponse res, String id){
        int i = Integer.valueOf(id);
        Map<String, Object> config = appService.getConfig(i);
        printOK(res,"success",config);
        // ajax json 数据
    }

    @RequestMapping("/system/cities")
    public void province(HttpServletResponse res,String code){
        List<Map<String, Object>> mapList = cityService.cityList(code);
        printOK(res,"success",mapList);
    }

    @RequestMapping("/channelCity")
    public void cityForChannel(HttpServletResponse res,String code){
        List<Map<String, Object>> mapList = cityService.cityList(code);
        printOK(res,"success",mapList);
    }

    /**
     * 新建，修改数据
     */
    @RequestMapping("/system/bannerInfo")
    public void bannerInfo(HttpServletResponse res, Form form) throws IOException {
        int change ;
        if(StringUtil.hasLength(form.getId())){
            change = Integer.valueOf(form.getId()) ;
            bannerService.updateBanner(form) ;
        }else {
            change = bannerService.newBanner(form) ;
        }
        //   json 数据
        res.sendRedirect("/system/banners?change="+change);
    }

    @RequestMapping("/system/configInfo")
    public void changeConfig(HttpServletResponse res, Form form) throws IOException {
        int change ;
        if(StringUtil.hasLength(form.getId())){
            change = Integer.valueOf(form.getId()) ;
            appService.updateConfig(form) ;
        }else {
            change = appService.newConfig(form) ;
        }
        res.sendRedirect("/system/config?change="+change);
    }

    @RequestMapping("/system/delConfig")
    public void delConfig(HttpServletResponse res,int id) throws IOException {
        appService.delConfig(id) ;
        res.sendRedirect("/system/config");
    }

}
